import pygame
import random
from pygame.locals import *


X=0
Y=1
PATCH_SIZE = (6,6)
COLOR_ARVORE = (0,255,0)
COLOR_FOGO = (255,0,0)
COLOR_CLAREIRA = (126,62,0)
COLOR_FOGO_IMINENTE = (255,255,0)
COLOR_AGUA = (0,0,255)


class Patch:
    
    def __init__(self,SCREEN,coord,g):
        self.SCREEN = SCREEN
        self.coord = coord
        self.defForma()
        self.g = g
    
    def setG(self,g):
        self.g = g 

    def aprender(self,val):
        self.g = min(1,self.g*val)
        
            
    def gerarArvore(self):
        if random.random() <= self.g:
            return True
        else:
            return False
    
    def gerarFogoNatural(self,f):
        if random.random() <= f:  
            return True
        else:
            return False
    
    def gerarFogoPropagado(self,r):
        if random.random() > r: 
            return True
        else:
            return False
                
        
    def genCoordNeigh8(self,grid_size):
        xp1 = (self.coord[X] + 1) % grid_size[X]
        xm1 = (self.coord[X] - 1) % grid_size[X]
        yp1 = (self.coord[Y] + 1) % grid_size[Y]
        ym1 = (self.coord[Y] - 1) % grid_size[Y]
        list8 = [(xm1,ym1),(self.coord[X],ym1),(xp1,ym1),
                 (xm1,self.coord[Y]),(xp1,self.coord[Y]),
                 (xm1,yp1),(self.coord[X],yp1),(xp1,yp1)]
        return list8
    
    def genCoordNeigh4(self,grid_size):
        xp1 = (self.coord[X] + 1) % grid_size[X]
        xm1 = (self.coord[X] - 1) % grid_size[X]
        yp1 = (self.coord[Y] + 1) % grid_size[Y]
        ym1 = (self.coord[Y] - 1) % grid_size[Y]
        list8 = [(self.coord[X],ym1),
                 (xm1,self.coord[Y]),(xp1,self.coord[Y]),
                 (self.coord[X],yp1)]
        return list8
    
    def setNeigh8(self,neighLst):
        self.neigh8 = neighLst
    
    def setNeigh4(self,neighlst):
        self.neigh4 = neighlst
    
    def setNeigh4X(self,neighlst):
        self.neigh4X = neighlst
    
    def getNeigh8(self):
        return self.neigh8
    
    def getNeigh4(self):
        return self.neigh4
    
    def getNeigh4X(self):
        return self.neigh4X
    
    def getNeigh16(self):
        return self.neigh16
    
    def setNeigh16(self,neighlst):
        self.neigh16 = neighlst
   
    
    def defForma(self):
        #self.forma = Rect(self.x*PATCH_SIZE[X],self.y*PATCH_SIZE[Y],PATCH_SIZE[X],PATCH_SIZE[Y])
        #pygame.draw.rect(self.SCREEN,COLOR_OFF,self.forma,3)
        self.raio = PATCH_SIZE[X]/2
        self.coordenada = (self.coord[X]*PATCH_SIZE[X]+self.raio,self.coord[Y]*PATCH_SIZE[Y]+self.raio)
        
    
    def desenhar(self,color):
        #pygame.draw.rect(self.SCREEN,color,self.forma,3)
       
        pygame.draw.circle(self.SCREEN,color,self.coordenada,self.raio,3)
    